/**
 *
 */
package com.sanhai.android.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.BaseAdapter;

/**
 * @author mahuihuang
 */
public abstract class EduListAdapter<T> extends BaseAdapter {

    protected LayoutInflater layoutInflater = null;

    protected Context context = null;
    protected List<T> data = null;

    public EduListAdapter(Context context, List<T> data) {
        this.context = context;
        this.data = data;

        this.layoutInflater = LayoutInflater.from(this.context);
    }

    /**
     * 获得列表中的数据
     *
     * @return
     */
    public List<T> getData() {
        return data;
    }

    /**
     * 返回指定下标的数据
     *
     * @param index
     * @return
     */
    public T get(int index) {
        if (this.data == null) {
            return null;
        }
        if (index >= this.data.size()) {
            Log.e("EduListAdapter", "ListView下标越界");
            return null;
        }
        return this.data.get(index);
    }

    /**
     * 设置列表中的数据
     *
     * @param data
     */
    public void setData(List<T> data) {
        this.data = data;
        this.notifyDataSetChanged();
    }

    public void addData(List<T> d) {
        if (d == null) {
            return;
        }
        if (this.data == null) {
            this.data = new ArrayList<T>();
        }
        this.data.addAll(d);
        this.notifyDataSetChanged();
    }

    /**
     * 添加列表中的数据
     *
     * @param object
     */
    public void addData(T object) {
        if (object == null) {
            return;
        }
        if (this.data == null) {
            this.data = new ArrayList<T>();
        }
        this.data.add(object);
        this.notifyDataSetChanged();
    }

    public void clearData() {
        if (this.data != null) {
            this.data.clear();
            this.notifyDataSetChanged();
        }
    }

    /*
     * (non-Javadoc)
     *
     * @see android.widget.Adapter#getCount()
     */
    @Override
    public int getCount() {
        if (this.data == null)
            return 0;
        return this.data.size();
    }

    /*
     * (non-Javadoc)
     *
     * @see android.widget.Adapter#getItem(int)
     */
    @Override
    public Object getItem(int arg0) {
        if (this.data == null)
            return null;
        return this.data.get(arg0);
    }

    /*
     * (non-Javadoc)
     *
     * @see android.widget.Adapter#getItemId(int)
     */
    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

}
